package cn.ibizlab.odoo.core.odoo_event.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Ievent_type_mail;
import cn.ibizlab.odoo.core.odoo_event.domain.Event_type_mail;
import cn.ibizlab.odoo.core.odoo_event.filter.Event_type_mailSearchContext;
import cn.ibizlab.odoo.core.odoo_event.service.IEvent_type_mailService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[基于活动分类的邮件调度] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Event_type_mailServiceImpl implements IEvent_type_mailService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Ievent_type_mailClientService event_type_mailClientService;

    @Override
    public void updateBatch(List<Event_type_mail> event_type_mails, int batchSize) {

        //event_type_mailClientService.updateBatch(event_type_mails,batchSize) ;

    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Ievent_type_mail clientModel = event_type_mailClientService.createModel();
        clientModel.setId(id);
        event_type_mailClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public boolean create(Event_type_mail event_type_mail) {
        boolean bOk = false ;
        Ievent_type_mail clientModel = this.convert2Model(event_type_mail,null);
        event_type_mailClientService.create(clientModel) ;
        this.convert2Domain(clientModel,event_type_mail);
        bOk = true ;

        return bOk ;
    }

    @Override
    public boolean update(Event_type_mail event_type_mail) {
        boolean bOk = false ;
        Ievent_type_mail clientModel = this.convert2Model(event_type_mail,null);
        event_type_mailClientService.update(clientModel) ;
        this.convert2Domain(clientModel,event_type_mail) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public Event_type_mail get(Integer id) {
        Ievent_type_mail clientModel = event_type_mailClientService.createModel();
        clientModel.setId(id);
        event_type_mailClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Event_type_mail get(Event_type_mail event_type_mail) {
        Event_type_mail retevent_type_mail = this.get(event_type_mail.getId()) ;
    	BeanCopier copier=BeanCopier.create(retevent_type_mail.getClass(), event_type_mail.getClass(), false);
        copier.copy(retevent_type_mail, event_type_mail, null);
        return event_type_mail ;
    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //event_type_mailClientService.removeBatch(event_type_mails) ;

    }

    @Override
    public void createBatch(List<Event_type_mail> event_type_mails, int batchSize) {

                        //event_type_mailClientService.createBatch(event_type_mails,batchSize) ;

    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Event_type_mail> searchDefault(Event_type_mailSearchContext context) {
        List<Event_type_mail> list = new ArrayList<Event_type_mail>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_type_mail> clientModelList = event_type_mailClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_type_mail clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_type_mail>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据活动类别查询
     */
    @Override
    public Page<Event_type_mail> selectByOdoo_event_type(Integer id){
        Event_type_mailSearchContext context = new Event_type_mailSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_type_mail> list = new ArrayList<Event_type_mail>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_type_mail> clientModelList = event_type_mailClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_type_mail clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_type_mail>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据EMail模板查询
     */
    @Override
    public Page<Event_type_mail> selectByOdoo_template(Integer id){
        Event_type_mailSearchContext context = new Event_type_mailSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_type_mail> list = new ArrayList<Event_type_mail>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_type_mail> clientModelList = event_type_mailClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_type_mail clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_type_mail>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Event_type_mail> selectByOdoo_create(Integer id){
        Event_type_mailSearchContext context = new Event_type_mailSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_type_mail> list = new ArrayList<Event_type_mail>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_type_mail> clientModelList = event_type_mailClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_type_mail clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_type_mail>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Event_type_mail> selectByOdoo_write(Integer id){
        Event_type_mailSearchContext context = new Event_type_mailSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Event_type_mail> list = new ArrayList<Event_type_mail>();
        Page<cn.ibizlab.odoo.core.client.model.Ievent_type_mail> clientModelList = event_type_mailClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Ievent_type_mail clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Event_type_mail>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Ievent_type_mail convert2Model(Event_type_mail domain , Ievent_type_mail model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = event_type_mailClientService.createModel() ;
        }

         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.getInterval_typeDirtyFlag())
            model.setInterval_type(domain.getInterval_type());
         if(domain.getInterval_unitDirtyFlag())
            model.setInterval_unit(domain.getInterval_unit());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getInterval_nbrDirtyFlag())
            model.setInterval_nbr(domain.getInterval_nbr());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getEvent_type_id_textDirtyFlag())
            model.setEvent_type_id_text(domain.getEvent_type_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getTemplate_id_textDirtyFlag())
            model.setTemplate_id_text(domain.getTemplate_id_text());
         if(domain.getEvent_type_idDirtyFlag())
            model.setEvent_type_id(domain.getEvent_type_id());
         if(domain.getTemplate_idDirtyFlag())
            model.setTemplate_id(domain.getTemplate_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
        return model ;
    }

    public Event_type_mail convert2Domain( Ievent_type_mail model ,Event_type_mail domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Event_type_mail();
        }

        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.getInterval_typeDirtyFlag())
            domain.setInterval_type(model.getInterval_type());
        if(model.getInterval_unitDirtyFlag())
            domain.setInterval_unit(model.getInterval_unit());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getInterval_nbrDirtyFlag())
            domain.setInterval_nbr(model.getInterval_nbr());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getEvent_type_id_textDirtyFlag())
            domain.setEvent_type_id_text(model.getEvent_type_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getTemplate_id_textDirtyFlag())
            domain.setTemplate_id_text(model.getTemplate_id_text());
        if(model.getEvent_type_idDirtyFlag())
            domain.setEvent_type_id(model.getEvent_type_id());
        if(model.getTemplate_idDirtyFlag())
            domain.setTemplate_id(model.getTemplate_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        return domain ;
    }


}
